package com.video

import android.content.Context
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Build


/**
 * @author ybk
 * @date :2020/2/9 7:21 PM
 * @desc:
 */
class FlashLightUtils(val context: Context) {

    var mCameraManager: CameraManager =
        context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    var mCameraId: String = ""

    init {
        try {
            mCameraId = mCameraManager.cameraIdList[0]
        } catch (e: CameraAccessException) {
            e.printStackTrace();
        }
    }

    fun open() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                mCameraManager.setTorchMode(mCameraId, true);//开启camera闪光灯
            } catch (e: CameraAccessException) {
                e.printStackTrace()
            }
        }
    }

    fun close() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                mCameraManager.setTorchMode(mCameraId, false);//关闭camera闪光灯
            } catch (e: CameraAccessException) {
                e.printStackTrace()
            }
        }
    }

}